package com.library.entities;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Entity
@Table(name = "ACCOUNT")
public class Account {
	
	@Id
	@GeneratedValue
	@Column(name = "ACCOUNT_ID")
	private Integer id;

	@Column(name = "LOGIN")
	@Size(min=4, max=20,message=
			"Username must be between 4 and 20 characters long")
	@Pattern(regexp="^[a-zA-Z0-9]+$", message=
			"Username must be alphanumeric with no spaces")
	private String username;

	@Column(name = "PASSWORD")
	@Size(min=6, max=20, message="The password must be at least 6 characters long")
	private String password;
	
	@OneToOne(mappedBy = "account")
	private User user;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="ACCOUNT_ROLE",
		joinColumns = @JoinColumn(name="ACCOUNT_ID", referencedColumnName="ACCOUNT_ID"),
		inverseJoinColumns = @JoinColumn(name="ROLE_ID", referencedColumnName="ROLE_ID")
	)
	private List<Role> roles;

	public String getUsername() {
		return username;
	}

	public void setUsername(String userName) {
		this.username = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	public Integer getId(){
		return id;
	}
	
	public void setId(Integer id){
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}
